# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Xin Sun <sun.simpson@gmail.com>

_realname=pyside
pkgbase=mingw-w64-${_realname}-qt4
pkgname=(${MINGW_PACKAGE_PREFIX}-${_realname}-common-qt4 ${MINGW_PACKAGE_PREFIX}-python{2,3}-${_realname}-qt4)
pkgver=1.2.2
pkgrel=4
arch=('any')
url="https://pyside.github.io/docs/pyside/"
license=("LGPL")
install=${_realname}-${CARCH}.install
source=(https://pypi.python.org/packages/source/P/PySide/PySide-${pkgver}.tar.gz
        pyside.patch)
sha256sums=('53129fd85e133ef630144c0598d25c451eab72019cdcb1012f2aec773a3f25be'
            '7e8e3c26a8ab2f279260965cb44ef7e8b8c5f3093898dc9fff6288ef024d44d9')
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-python2-shiboken-qt4"
             "${MINGW_PACKAGE_PREFIX}-python3-shiboken-qt4"
             "${MINGW_PACKAGE_PREFIX}-qt4")
options=('staticlibs' 'strip')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i ${srcdir}/pyside.patch
}

build() {
  for pyv in python2 python3; do
    local _conf=
    if [ "${pyv}" = "python2" ]; then
      _conf="-DPYTHON_SUFFIX=-python2.7"
    fi
    [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
    cp -rf PySide-${pkgver} build-${MINGW_CHOST}-${pyv}
    pushd ${srcdir}/build-${MINGW_CHOST}-${pyv}/sources/pyside > /dev/null

    export MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX"
    ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=RELEASE \
      ${_conf} \
      -DPYTHON_EXECUTABLE=${MINGW_PREFIX}/bin/${pyv}.exe

    make

    popd > /dev/null
  done

}

fix_paths() {
  local prefixwin=$(cygpath -am ${MINGW_PREFIX})
  local prefixinst=$(cygpath -am ${pkgdir}${MINGW_PREFIX})

  local _files=$(find ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver} -type f -iname "*.cmake" -o -iname "*.pc")
  for f in ${_files}; do
    plain "Patching ${f}..."
    sed -e "s|${prefixwin}|${MINGW_PREFIX}|g" \
        -e "s|${prefixinst}|${MINGW_PREFIX}|g" \
        -i ${f}
  done
}

package_pyside-common() {
  pkgdesc="Provides LGPL Qt bindings for Python and related tools for binding generation (Common files)(mingw-w64)"

  cd ${srcdir}/build-${MINGW_CHOST}-python3/sources/pyside
  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/bin
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/python*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/libpyside*
  rm -rf ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver}/PySideConfig*python*.cmake

  fix_paths
}

package_python2-pyside() {
  pkgdesc="Provides LGPL Qt bindings for Python and related tools for binding generation (Python2)(mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside-common-qt4"
           "${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-shiboken-qt4"
           "${MINGW_PACKAGE_PREFIX}-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python2/sources/pyside
  make DESTDIR=${pkgdir} install

  #install -Dm644 PySideConfig-python2.7.cmake ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver}/
  mv ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig/pyside{,-py2}.pc

  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver}/PySideConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver}/PySideConfig.cmake

  fix_paths
}

package_python3-pyside() {
  pkgdesc="Provides LGPL Qt bindings for Python and related tools for binding generation (Python3)(mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-pyside-common-qt4"
           "${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-shiboken-qt4"
           "${MINGW_PACKAGE_PREFIX}-qt4")

  cd ${srcdir}/build-${MINGW_CHOST}-python3/sources/pyside
  make DESTDIR=${pkgdir} install

  rm -rf ${pkgdir}${MINGW_PREFIX}/{include,share}
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver}/PySideConfigVersion.cmake
  rm ${pkgdir}${MINGW_PREFIX}/lib/cmake/PySide-${pkgver}/PySideConfig.cmake

  fix_paths
}

# Wrappers
package_mingw-w64-i686-pyside-common-qt4() {
  package_pyside-common
}

package_mingw-w64-i686-python2-pyside-qt4() {
  package_python2-pyside
}

package_mingw-w64-i686-python3-pyside-qt4() {
  package_python3-pyside
}

package_mingw-w64-x86_64-pyside-common-qt4() {
  package_pyside-common
}

package_mingw-w64-x86_64-python2-pyside-qt4() {
  package_python2-pyside
}

package_mingw-w64-x86_64-python3-pyside-qt4() {
  package_python3-pyside
}
